package com.jeesite.modules.kj.api.controller.integrals;


import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.shiro.realms.Pa;
import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.vo.DataListPageVo;
import com.jeesite.modules.kj.api.vo.response.IntegralHistoryResponseVo;
import com.jeesite.modules.kj.api.vo.response.IntegralResponseVo;
import com.jeesite.modules.kj.api.vo.response.MemberJobResponseVo;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import com.jeesite.modules.kj.entity.*;
import com.jeesite.modules.kj.exception.FreezMemberException;
import com.jeesite.modules.kj.service.KjMemberIntegralHistoryService;
import com.jeesite.modules.kj.service.KjMemberIntegralsService;
import com.jeesite.modules.kj.utils.KjMemberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.List;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:
 */
@Api(value = "积分api", tags = {"积分API"})
@Slf4j
@Controller
@RequestMapping(value = "/api/integrals")
public class IntegralApiController {

    @Autowired
    KjMemberIntegralsService kjMemberIntegralsService;

    @Autowired
    KjMemberIntegralHistoryService kjMemberIntegralHistoryService;


    @ApiOperation(value = "爱心积分排行", notes = "积分排行")
    @GetMapping(value = "rank")
    public ResponseBean<List<MemberIntegralRank>> rank(String myCity, int pageNo, int pageSize) {
        KjMemberIntegralHistory where = new KjMemberIntegralHistory();
        if(StringUtils.isNotBlank(myCity)){
            where.setMyCity(myCity);
        }
        Page<KjMemberIntegralHistory> page = new Page(pageNo, pageSize);
        where.setPage(page);
        List<MemberIntegralRank> result = kjMemberIntegralHistoryService.getMemberIntegralRank(where);
        return new ResponseBean<>(result);
    }


    @ApiOperation(value = "我的积分排行", notes = "我的积分排行")
    @GetMapping(value = "myIntegralRank")
    @UserLoginToken
    public ResponseBean<MemberIntegralRank> myIntegralRank(@ApiIgnore @CurrentUser String memberId, String myCity) {
        KjMemberIntegralHistory where = new KjMemberIntegralHistory();
        if(StringUtils.isNotBlank(myCity)){
            where.setMyCity(myCity);
        }
        KjMember member = new KjMember();
        member.setId(memberId);
        where.setKjMember(member);
        MemberIntegralRank rank = kjMemberIntegralHistoryService.memberIntegralRankNum(where);
        return new ResponseBean<>(rank);
    }


    @ApiOperation(value = "爱心捐赠积分排行", notes = "积分捐赠排行")
    @GetMapping(value = "contributeRank")
    public ResponseBean<List<MemberIntegralRank>> contributeRank(String myCity, int pageNo, int pageSize) {
        KjMemberIntegralHistory where = new KjMemberIntegralHistory();
        if(StringUtils.isNotBlank(myCity)){
            where.setMyCity(myCity);
        }
        Page<KjMemberIntegralHistory> page = new Page(pageNo, pageSize);
        where.setPage(page);
        List<MemberIntegralRank> vos = kjMemberIntegralHistoryService.getMemberContributeRank(where);
        return new ResponseBean<>(vos);
    }



    @ApiOperation(value = "我的捐赠积分排行", notes = "我的捐赠积分排行")
    @GetMapping(value = "myContributeRank")
    @UserLoginToken
    public ResponseBean<MemberIntegralRank> myContributeRank(@ApiIgnore @CurrentUser String memberId, String myCity) throws FreezMemberException {
        KjMemberIntegralHistory where = new KjMemberIntegralHistory();
        KjMember member = KjMemberUtil.getCurrentMember(memberId);
        where.setKjMember(member);
        where.setMyCity(myCity);
        MemberIntegralRank rank = kjMemberIntegralHistoryService.memberContributeRankNum(where);
        return new ResponseBean<>(rank);
    }

    @ApiOperation(value = "团队积分捐赠排行", notes = "团队积分捐赠排行")
    @GetMapping(value = "teamContributeRank")
    public ResponseBean<List<TeamContributeRank>> teamContributeRank(String myCity, int pageNo, int pageSize) {
        KjMemberIntegralHistory where = new KjMemberIntegralHistory();
        if(StringUtils.isNotBlank(myCity)){
            where.setMyCity(myCity);
        }
        Page<KjMemberIntegralHistory> page = new Page(pageNo, pageSize);
        where.setPage(page);
        List<TeamContributeRank> vos = kjMemberIntegralHistoryService.getTeamContributeRank(where);
        return new ResponseBean<>(vos);
    }

    @ApiOperation(value = "我的积分统计", notes = "我的积分统计")
    @GetMapping(value = "my")
    @UserLoginToken
    public ResponseBean<IntegralResponseVo> my(@ApiIgnore @CurrentUser String memberId) {
        KjMemberIntegrals integrals = kjMemberIntegralsService.findByMemberId(memberId);
        return new ResponseBean<>(new IntegralResponseVo(integrals));
    }


    @ApiOperation(value = "我的积分历史", notes = "我的积分历史")
    @GetMapping(value = "history")
    @UserLoginToken
    public ResponseBean history(@ApiIgnore @CurrentUser String memberId, String integralType, int style, int pageNo, int pageSize) throws FreezMemberException {
        KjMemberIntegralHistory where = new KjMemberIntegralHistory();
        KjMember curMember = KjMemberUtil.getCurrentMember(memberId);
        where.setKjMember(curMember);
        where.setIntegralType(integralType);
        BigDecimal zero=new BigDecimal("0");
        switch (style){
            case 1:
                where.setIntegral_gte(zero);
                break;
            case 2:
                where.setIntegral_lte(zero);
                break;
        }
        Page<KjMemberIntegrals> page = new Page(pageNo, pageSize);
        where.setPage(page);
        Page<KjMemberIntegralHistory> dataList = kjMemberIntegralHistoryService.findPage(where);
//        DataListPageVo<IntegralHistoryResponseVo> result = new DataListPageVo<>(dataList, IntegralHistoryResponseVo.class);
        return new ResponseBean(dataList);
    }

}
